﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;

namespace Console_HanNuo
{
    class Program
    {
        static Stack<int> leftColumn = new Stack<int>();
        static Stack<int> centerColumn = new Stack<int>();
        static Stack<int> rightColumn = new Stack<int>();

        static void Main(string[] args)
        {
            InitializeCollection2();
            Regex colRegex = new Regex(@"^[123]$");
            Console.WriteLine("***** Game Begin *****");
            int rol = 1;

            int[] counts1 = new int[] { leftColumn.Count, centerColumn.Count, rightColumn.Count };
            var useLeftColumn1 = leftColumn.ToList();
            var useCenterColumn1 = centerColumn.ToList();
            var useRightColumn1 = rightColumn.ToList();

            for (int i = 0; i < counts1.Max(); i++)
            {
                var v1 = i >= counts1.Max() - useLeftColumn1.Count ? useLeftColumn1[i - counts1.Max() + useLeftColumn1.Count].ToString() : "|";
                var v2 = i >= counts1.Max() - useCenterColumn1.Count ? useCenterColumn1[i - counts1.Max() + useCenterColumn1.Count].ToString() : "|";
                var v3 = i >= counts1.Max() - useRightColumn1.Count ? useRightColumn1[i - counts1.Max() + useRightColumn1.Count].ToString() : "|";

                Console.WriteLine(string.Format("{0}\t{1}\t{2}", v1, v2, v3));
            }

            while (true)
            {
                bool isInputPass = false;
                string sourceColumn = null;
                string targetColumn = null;
                int? sourceValue = null;

                #region Get Info from user
                Console.WriteLine();
                Console.WriteLine(string.Format("***** Rol: {0}*****", rol));
                rol++;

                do
                {
                    Console.Write("Please select source column: ");
                    var inputString = Console.ReadLine();

                    if (colRegex.IsMatch(inputString) && IsContainsItemByID(inputString) && isBetween(inputString))
                    {
                        sourceColumn = inputString;
                        isInputPass = true;
                    }
                    else
                    {
                        isInputPass = false;

                        if (!colRegex.IsMatch(inputString))
                        {
                            Console.WriteLine("Error! Please select column in (1, 2, 3)");
                        }
                        else if (!IsContainsItemByID(inputString))
                        {
                            Console.WriteLine("Error! Sorry, source column doesn't have any item.");
                        }
                        else
                        {
                            Console.WriteLine("Error! Sorry, you can't select this column.");
                        }
                    }

                } while (!isInputPass);

                Console.WriteLine();
                do
                {
                    Console.Write("Please select target column: ");
                    var inputString = Console.ReadLine();

                    if (colRegex.IsMatch(inputString) && (targetColumn = inputString) != sourceColumn && ColumnValueByID(sourceColumn) < ColumnValueByID(inputString))
                    {
                        isInputPass = true;
                    }
                    else
                    {
                        isInputPass = false;

                        if (!colRegex.IsMatch(inputString))
                        {
                            Console.WriteLine("Error! Please select column in (1, 2, 3)");
                        }
                        else if (targetColumn == sourceColumn)
                        {
                            Console.WriteLine("Error! Target column can not be source column");
                        }
                        else if (ColumnValueByID(sourceColumn) >= ColumnValueByID(inputString))
                        {
                            Console.WriteLine("Error! source column can not bigger than target column");
                        }
                    }

                } while (!isInputPass);

                #endregion

                #region control
                switch (sourceColumn)
                {
                    case "1":
                        sourceValue = leftColumn.Pop();
                        break;
                    case "2":
                        sourceValue = centerColumn.Pop();
                        break;
                    case "3":
                        sourceValue = rightColumn.Pop();
                        break;
                    default:
                        break;
                }

                switch (targetColumn)
                {
                    case "1":
                        leftColumn.Push(sourceValue.Value);
                        break;
                    case "2":
                        centerColumn.Push(sourceValue.Value);
                        break;
                    case "3":
                        rightColumn.Push(sourceValue.Value);
                        break;
                    default:
                        break;
                }

                #endregion

                #region display in ui
                Console.WriteLine();

                int[] counts = new int[] { leftColumn.Count, centerColumn.Count, rightColumn.Count };
                var useLeftColumn = leftColumn.ToList();
                var useCenterColumn = centerColumn.ToList();
                var useRightColumn = rightColumn.ToList();

                for (int i = 0; i < counts.Max(); i++)
                {
                    var v1 = i >= counts.Max() - useLeftColumn.Count ? useLeftColumn[i - counts.Max() + useLeftColumn.Count].ToString() : "|";
                    var v2 = i >= counts.Max() - useCenterColumn.Count ? useCenterColumn[i - counts.Max() + useCenterColumn.Count].ToString() : "|";
                    var v3 = i >= counts.Max() - useRightColumn.Count ? useRightColumn[i - counts.Max() + useRightColumn.Count].ToString() : "|";

                    Console.WriteLine(string.Format("{0}\t{1}\t{2}", v1, v2, v3));
                }

                if (leftColumn.Count == 0 && centerColumn.Count == 0)
                {
                    Console.WriteLine("Winner");
                    break;
                }

                #endregion
            }

            Console.ReadLine();
        }

        private static bool isBetween(string inputString)
        {
            var v1 = ColumnValueByID(inputString);

            string[] IDs = new string[] { "1", "2", "3" };

            var useIDValues = IDs.Where(x => x != inputString).Select(x => ColumnValueByID(x));

            if (useIDValues.Max() >= v1)
            {
                return true;
            }
            else
                return false;
        }

        static void InitializeCollection()
        {
            Regex colRegex = new Regex(@"^[1-9]\d*$");

            bool isInputPass = false;
            do
            {
                Console.WriteLine("Please input the level:");
                var inputString = Console.ReadLine();

                if (colRegex.IsMatch(inputString))
                {
                    isInputPass = true;

                    for (int i = 0; i < int.Parse(inputString); i++)
                    {
                        leftColumn.Push(int.Parse(inputString) - i);
                    }
                }
                else
                {
                    Console.WriteLine("Error: Please Input the integer (bigger than 1) number.");
                    isInputPass = false;
                }


            } while (!isInputPass);


        }

        static void InitializeCollection2()
        {
            leftColumn.Push(7);
            leftColumn.Push(5);
            leftColumn.Push(2);

            centerColumn.Push(4);
            centerColumn.Push(3);
            centerColumn.Push(1);

            rightColumn.Push(9);
            rightColumn.Push(8);
            rightColumn.Push(6);


        }

        static bool IsContainsItemByID(string ID)
        {
            switch (ID)
            {
                case "1":
                    return leftColumn.Count > 0 ? true : false;
                case "2":
                    return centerColumn.Count > 0 ? true : false;
                case "3":
                    return rightColumn.Count > 0 ? true : false;
                default:
                    return false;
            }
        }

        static int ColumnValueByID(string ID)
        {
            switch (ID)
            {
                case "1":
                    return leftColumn.Count > 0 ? leftColumn.Peek() : int.MaxValue;
                case "2":
                    return centerColumn.Count > 0 ? centerColumn.Peek() : int.MaxValue;
                case "3":
                    return rightColumn.Count > 0 ? rightColumn.Peek() : int.MaxValue;
                default:
                    return int.MaxValue;
            }
        }
    }
}
